package com.xyh.web.admin;

import com.xyh.pojo.Blog;
import com.xyh.pojo.PageRequest;
import com.xyh.pojo.PageResult;
import com.xyh.pojo.User;
import com.xyh.service.BlogService;
import com.xyh.service.TagService;
import com.xyh.service.TypeService;
import com.xyh.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @GetMapping("/blogs")
    public String blogs(PageRequest pageQuery, BlogQuery blog,Model model) {
        model.addAttribute("types",typeService.getAll());
        PageResult page = blogService.getPageByCondition(pageQuery,blog);
        model.addAttribute("page",page);
        return "admin/blogs";
    }
    @PostMapping("/blogs/search")
    public String search(PageRequest pageQuery, BlogQuery blog,Model model) {
        PageResult page = blogService.getPageByCondition(pageQuery,blog);
        model.addAttribute("page",page);
        return "admin/blogs::blogList";
    }
    @GetMapping("/blogs/input")
    public String input(Model model) {
        model.addAttribute("blog",new Blog());
        setTypesAndTags(model);
        return "admin/blogs-input";
    }

    private void setTypesAndTags(Model model) {
        model.addAttribute("types", typeService.getAll());
        model.addAttribute("tags", tagService.getAll());
    }

    @GetMapping("/blog/input/{id}")
    public String editInput(@PathVariable Long id, Model model) {
        setTypesAndTags(model);
        model.addAttribute("blog",blogService.getBlog(id));
        return "admin/blogs-input";
    }
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.getTagInIds(blog.getTagIds()));
        System.out.println("blog = " + blog);
        int i = blogService.save(blog);
        if (i>=1){
            attributes.addFlashAttribute("message","操作成功！");
        }else {
            attributes.addFlashAttribute("message", "操作失败！");
        }
        return "redirect:/admin/blogs";
    }
    @GetMapping("/blog/delete/{id}")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        int delete = blogService.delete(id);
        if (delete>=1) {
            attributes.addFlashAttribute("message", "删除成功！");
        }else {
            attributes.addFlashAttribute("message", "删除失败！");
        }
        return "redirect:/admin/blogs";
    }
}
